package com.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class DAPolicyServerSocket extends Thread {

	private ServerSocket server = null;
	private boolean start = true;

	private static DAPolicyServerSocket pss = null;

	/* Get The Single DAPolicyServerSocket Object.Single Pattern */
	public static DAPolicyServerSocket getInstance() {
		if (DAPolicyServerSocket.pss == null) {
			DAPolicyServerSocket.pss = new DAPolicyServerSocket();
		}
		return DAPolicyServerSocket.pss;
	}

	private DAPolicyServerSocket() {
		try {
			if (server == null) {
				server = new ServerSocket(843);
			} else {
				System.err.println("The server aleady running...");
			}
		} catch (IOException e) {
			e.printStackTrace();
			System.err.println("Init policy server failed...");
		}
	}

	public void run() {
		try {
			while (start) {
				// create a thread to send policy file to client
				new SendPolicyFile(server.accept()).start();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (server != null)
					server.close();
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}
		}
	}

	private class SendPolicyFile extends Thread {
		private Socket socket = null;
		private BufferedReader reader = null;
		private PrintWriter writer = null;
		private StringBuffer message = null;

		public SendPolicyFile(Socket soc) {
			this.socket = soc;
			message = new StringBuffer();
		}

		public void run() {
			try {
				reader = new BufferedReader(new InputStreamReader(socket
						.getInputStream()));
				char[] ch = new char[22];
				reader.read(ch, 0, ch.length);
				for (int i = 0; i < ch.length; i++) {
					message.append(ch[i]);
				}
				if (message.toString().indexOf("<policy-file-request/>") != -1) {
					writer = new PrintWriter(socket.getOutputStream(), true);
					writer
							.write("<cross-domain-policy>"
									+ "<allow-access-from domain = \"10.2.3.56\" to-ports=\"8888\" />"
									+ "<allow-access-from domain = \"localhost\" to-ports=\"8888\" />"
									+ "<allow-access-from domain = \"127.0.0.1\" to-ports=\"8888\" />"
									+ "</cross-domain-policy>" + "\0");
					writer.flush();
				}
			} catch (IOException ioe) {
				ioe.printStackTrace();
			} finally {
				try {
					if (this.message != null)
						this.message = null;
					if (this.writer != null)
						this.writer.close();
					if (this.reader != null)
						this.reader.close();
					if (this.socket != null)
						this.socket.close();
				} catch (IOException ioe) {
					ioe.printStackTrace();
				}
			}
		}
	}

	public void stopServer() {
		try {
			if (server != null)
				server.close();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		} finally {
			System.err.println("In DAPolicyServerSocket stopServer...");
		}
	}
}
